<?php

/**
 * 物流查询接口
 */
function logistics($num,$com){
    //参数设置
    $post_data = array();
    $post_data["customer"] = '6B87E482441CD2AE1E3E84F48D94E7F8';
    $key= 'cqKCQioo1556' ;
    $post_data["param"] = "{'com':'$com','num':'$num'}";

    $url='http://poll.kuaidi100.com/poll/query.do';
    $post_data["sign"] = md5($post_data["param"].$key.$post_data["customer"]);
    $post_data["sign"] = strtoupper($post_data["sign"]);
    $o="";
    foreach ($post_data as $k=>$v)
    {
        $o.= "$k=".urlencode($v)."&";		//默认UTF-8编码格式
    }
    $post_data=substr($o,0,-1);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    // curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);


    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);

    $result = curl_exec($ch);
    $data = str_replace("\&quot;",'"',$result );
    $data = json_decode($data,true);
    return $data;
}

/**
 * 获取支付密码的加密字符串
 * @param unknown $password
 */
function getEncodePayPasswrod($password)
{
    $pwdEncode = substr(hash('sha512', self::PAY_PWD_KEY . $password), 0, 32);
    return $pwdEncode;
}

/**
 * 计算2个日期之间的天数
 * @param $date1  开始时间
 * @param $date2  结束时间
 */
function getdays($date1,$date2){

    $day = (strtotime($date2)-$date1)/24/3600;
    return floor($day);
}
/**
 * 将字符串转换为数组
 *
 * @param    string $data 字符串
 * @return    array    返回数组格式，如果，data为空，则返回空数组
 */
function string2array($data)
{
    $data = trim($data);
    if ($data == '') return array();
    if (strpos($data, 'array') === 0) {
        @eval("\$array = $data;");
    } else {
        if (strpos($data, '{\\') === 0) $data = stripslashes($data);
        $array = json_decode($data, true);
        if (strtolower(CHARSET) == 'gbk') {
            $array = mult_iconv("UTF-8", "GBK//IGNORE", $array);
        }
    }
    return $array;
}

/**
 * 将数组转换为字符串
 *
 * @param    array $data 数组
 * @param    bool $isformdata 如果为0，则不使用new_stripslashes处理，可选参数，默认为1
 * @return    string    返回字符串，如果，data为空，则返回空
 */
function array2string($data, $isformdata = 1)
{
    if ($data == '' || empty($data)) return '';

    if ($isformdata) $data = stripslashes($data);
    if (strtolower(CHARSET) == 'gbk') {
        $data = mult_iconv("GBK", "UTF-8", $data);
    }
    if (version_compare(PHP_VERSION, '5.3.0', '<')) {
        return addslashes(json_encode($data));
    } else {
        return addslashes(json_encode($data, JSON_FORCE_OBJECT));
    }
}

/**
 * 字符截取 支持UTF8/GBK
 * @param $string
 * @param $length
 * @param $dot
 */
function str_cut($string, $length, $dot = '...')
{
    //$string = iconv("UTF-8","GB2312",$string);
    $strlen = strlen($string);
    if ($strlen <= $length) {
        return $string;
    }

    $string = str_replace(array(' ', '&nbsp;', '&amp;', '&quot;', '&#039;', '&ldquo;', '&rdquo;', '&mdash;', '&lt;', '&gt;', '&middot;', '&hellip;'), array('∵', ' ', '&', '"', "'", '“', '”', '—', '<', '>', '·', '…'), $string);
    $strcut = '';
    //  if (strtolower(CHARSET) == 'utf-8') {
    $length = intval($length - strlen($dot) - $length / 3);
    $n = $tn = $noc = 0;
    while ($n < strlen($string)) {
        $t = ord($string[$n]);
        if ($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
            $tn = 1;
            $n++;
            $noc++;
        } elseif (194 <= $t && $t <= 223) {
            $tn = 2;
            $n += 2;
            $noc += 2;
        } elseif (224 <= $t && $t <= 239) {
            $tn = 3;
            $n += 3;
            $noc += 2;
        } elseif (240 <= $t && $t <= 247) {
            $tn = 4;
            $n += 4;
            $noc += 2;
        } elseif (248 <= $t && $t <= 251) {
            $tn = 5;
            $n += 5;
            $noc += 2;
        } elseif ($t == 252 || $t == 253) {
            $tn = 6;
            $n += 6;
            $noc += 2;
        } else {
            $n++;
        }
        if ($noc >= $length) {
            break;
        }
    }
    if ($noc > $length) {
        $n -= $tn;
    }
    $strcut = substr($string, 0, $n);
    $strcut = str_replace(array('∵', '&', '"', "'", '“', '”', '—', '<', '>', '·', '…'), array(' ', '&amp;', '&quot;', '&#039;', '&ldquo;', '&rdquo;', '&mdash;', '&lt;', '&gt;', '&middot;', '&hellip;'), $strcut);
    //  } else {
    //      $dotlen      = strlen($dot);
    //      $maxi        = $length-$dotlen-1;
    //      $current_str = '';
    //      $search_arr  = array('&', ' ', '"', "'", '“', '”', '—', '<', '>', '·', '…', '∵');
    //      $replace_arr = array('&amp;', '&nbsp;', '&quot;', '&#039;', '&ldquo;', '&rdquo;', '&mdash;', '&lt;', '&gt;', '&middot;', '&hellip;', ' ');

    //      $search_flip = array_flip($search_arr);
    //      for ($i = 0; $i < $maxi; $i++) {
    //          $current_str = ord($string[$i]) > 127?$string[$i].$string[++$i]:$string[$i];
    //          if (in_array($current_str, $search_arr)) {
    //              $key         = $search_flip[$current_str];
    //              $current_str = str_replace($search_arr[$key], $replace_arr[$key], $current_str);
    //          }
    //          $strcut .= $current_str;
    //      }
    //  }

    //$strcut = iconv("GB2312","UTF-8",$strcut);
    return $strcut . $dot;
}

/**
 * 对用户的密码进行加密
 * @param $password
 * @param $encrypt //传入加密串，在修改密码时做认证
 * @return array/password
 */
function password($password, $encrypt = '')
{
    $pwd = array();
    $pwd['encrypt'] = $encrypt ? $encrypt : create_randomstr();
    $pwd['password'] = md5(md5(trim($password)) . $pwd['encrypt']);
    return $encrypt ? $pwd['password'] : $pwd;
}

/**
 * 生成随机字符串
 * @param string $lenth 长度
 * @return string 字符串
 */
function create_randomstr($lenth = 6)
{
    return random($lenth, '123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ');
}

/**
 * 产生随机字符串
 *
 * @param    int $length 输出长度
 * @param    string $chars 可选的 ，默认为 0123456789
 * @return   string     字符串
 */
function random($length, $chars = '0123456789')
{
    $hash = '';
    $max = strlen($chars) - 1;
    for ($i = 0; $i < $length; $i++) {
        $hash .= $chars[mt_rand(0, $max)];
    }
    return $hash;
}

/**
 * Introduction: 生成无限极分类数组
 * Date: 15/11/16
 * Time: 10:23
 * Last date: 15/11/16 10:23
 * Last user: arpeng
 * @param  array $items 无限极分类一维数组
 * @param  $name  string 子级下标
 */
function generateTree($items, $name = 'child')
{
//    foreach($rule as $v)
//        $rules[$v['id']] = $v;
    $tree = array();
    foreach ($items as $item) {
        if (isset($items[$item['pid']])) {
            $items[$item['pid']][$name][] = &$items[$item['id']];
        } else {
            $tree[] = &$items[$item['id']];
        }
    }
    return $tree;
}

/**
 * Introduction: 给图片连接添加参数,方便程序裁剪图片
 * Date: 15/12/13
 * Time: 20:31
 * Last date: 15/12/13 20:31
 * Last user: arpeng
 * @param  string $url 需要添加参数的URL
 * @param  int || string  $width  宽度
 * @param  int || string  $height  高度
 */
function cutUrl($url, $width, $height)
{
    if (!$url) return '';
    $info = pathinfo($url);
    return $info['dirname'] . '/' . $info['filename'] . '_' . $width . '_' . $height . '.' . $info['extension'];
}

/**
 * Introduction: 冒泡排序   用于blog  basic排序使用
 * Date: 15/12/14
 * Time: 18:28
 * Last date: 15/12/14 18:28
 * Last user: arpeng
 */
function bubbleSort($array)
{
    $count = count($array);
    for ($i = 0; $i < $count; $i++) {
        for ($j = 0; $j < $count - $i - 1; $j++) {
            if ($array[$j]['sort'] > $array[$j + 1]['sort']) {
                $temp = $array[$j];
                $array[$j] = $array[$j + 1];
                $array[$j + 1] = $temp;
            }
        }
    }
    return $array;
}

/**
 * Introduction: 获取注册时和登录时的用户名类型
 * Date: 16/01/30
 * Time: 12:28
 * Last date: 16/01/30 12:28
 * @param $sign String 登录时或者注册是输入的用户名
 */
function getSignType($sign)
{
    if (!$sign) return false;
    $return = [];
    $regxSign = [
        'mobile' => "/^1[3|4|5|6|7|8]\d{9}$/",
        'email' => "/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/",
        'username' => "/^[a-zA-Z0-9-_]+$/",
    ];
    foreach ($regxSign as $k => $v) {
        if (preg_match($v, $sign)) {
            $return[$k] = strtolower($sign);
            $return['type'] = $k;
            break;
        }
    }
    return $return;
}
/**
 * Introduction: 二维数组去重复
 * @param $data array 需要去重复的二维数组
 */
function unique($data = array()){

    $tmp = array();

    foreach($data as $key => $value){

        foreach($value as $key1 => $value1){

            $value[$key1] = $key1 . '_|_' . $value1;//_|_分隔符复杂点以免冲突

        }

        $tmp[$key] = implode(',|,', $value);//,|,分隔符复杂点以免冲突

    }
    $tmp = array_unique($tmp);

    $newArr = array();

    foreach($tmp as $k => $tmp_v){

        $tmp_v2 = explode(',|,', $tmp_v);

        foreach($tmp_v2 as $k2 => $v2){

            $v2 = explode('_|_', $v2);

            $tmp_v3[$v2[0]] = $v2[1];

        }

        $newArr[$k] = $tmp_v3;

    }

    return $newArr;

}

/**
 * 二维数组排序
 * @param array $list
 * @param string $field
 * @param string $sortby
 * @return array|boolean
 */
function listSortBy($list, $field, $sortby = 'asc')
{
    if (is_array($list)) {
        $refer = $resultSet = array();
        foreach ($list as $i => $data)
            $refer[$i] = $data[$field];
        switch ($sortby) {
            case 'asc': // 正向排序
                asort($refer);
                break;
            case 'desc': // 逆向排序
                arsort($refer);
                break;
            case 'nat': // 自然排序
                natcasesort($refer);
                break;
        }
        foreach ($refer as $key => $val)
            $resultSet[] = $list[$key];
        return $resultSet;
    }
    return false;
}
/**
 * 获取支付总价
 *
 * @param unknown_type $orderInfo
 *            订单信息
 */
function getPayPrice($orderInfo)
{
    $price = 0;
    $cost = isset($orderInfo['cost'])?$orderInfo['cost']:0;
    $totalPrice = isset($orderInfo['total_price'])?$orderInfo['total_price']:0;
    $cpnsPrice = isset($orderInfo['cpns_price'])?$orderInfo['cpns_price']:0;
    $freight = isset($orderInfo['freight'])?$orderInfo['freight']:0;
    $freightPrice = isset($orderInfo['freight_price'])?$orderInfo['freight_price']:0;
    $cashPrice = isset($orderInfo['cash_price'])?$orderInfo['cash_price']:0;
    $taxPrice = isset($orderInfo['tax_price'])?$orderInfo['tax_price']:0;
    $mstaxation = isset($orderInfo['total_ms_taxation'])?$orderInfo['total_ms_taxation']:0;
    $ms_Freight_risk = isset($orderInfo['ms_Freight_risk'])?$orderInfo['ms_Freight_risk']:0;
    if(!empty($orderInfo)){
        $price = ($cost ? $cost : $totalPrice) - $cpnsPrice + ($freight ? $freight : $freightPrice) + $mstaxation + $ms_Freight_risk + $taxPrice - $cashPrice;
    }

    return $price;
}